/**
 * 上海中赢金融信息服务有限公司
 * Copyright (c) 2016-2026 hmc,Inc.All Rights Reserved.
 */
package com.sys.midware.mongo.config;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.sys.midware.config.core.ConfigContainer;
import com.sys.midware.config.core.ConfigKey;
import com.sys.midware.config.model.BNode;
import com.sys.midware.logger.Logger;
import com.sys.midware.logger.LoggerFactory;

/**
 * 
 * <b>ClassName：</b> ConfigMongoUtil <br/>
 * <b>Description：</b> 获取配置信息工具类 <br/>
 * <b>@author：</b> mobing <br/>
 * <b>@date：</b> 2015年10月19日 下午2:55:02 <br/>
 * <b>@version: </b> <br/>
 */
public class MongoConfigUtil {
    private static final Logger logger = LoggerFactory.getLogger(MongoConfigUtil.class);

    /**
     * ConfigContainer instance
     */
    private static ConfigContainer cfg = ConfigContainer.getInstance(ConfigKey.ConfigFile.CONFIG_FILE_ROOT);

//    private static final Map<String, MongoConfig> mongoConfigCached = new HashMap<String, MongoConfig>();

    private static boolean inited;
    public static void initConfig() {
        if(inited){
            return;
        }
        logger.info("开始初始化mongo配置...");
        Map<String, MongoConfig> mongoConfigCached = new HashMap<String, MongoConfig>();
        List<BNode> bnodeList = cfg.getBNodeList();
        for (BNode b : bnodeList) {
            String nodeFor = b.getNodeFor();
            if("mongo".equals(nodeFor)){
                MongoConfig mc = new MongoConfig();
                mc.setNodeName(b.getNodeName());
                mc.setNodeType(b.getNodeType());
                mc.setHost(b.getNodeParamStr(ConfigKey.Mongo.KEY_mongo_host));
                mc.setPort(b.getNodeParamInteger(ConfigKey.Mongo.KEY_mongo_port));
                mc.setUserName(b.getNodeParamStr(ConfigKey.Mongo.KEY_mongo_userName));
                mc.setPassword(b.getNodeParamStr(ConfigKey.Mongo.KEY_mongo_password));
                mc.setAuth(b.getNodeParamBoolean(ConfigKey.Mongo.KEY_mongo_isAuth));
                mc.setAuthDB(b.getNodeParamStr(ConfigKey.Mongo.KEY_mongo_authDB));
                // mongo连接属性,从公共属性中获得
                mc.setAutoConnectRetry(cfg.getParamBoolean(ConfigKey.Mongo.KEY_mongo_autoConnectRetry));
                mc.setConnectionsPerHost(cfg.getParamInteger(ConfigKey.Mongo.KEY_mongo_connectionsPerHost));
                mc.setConnectTimeout(cfg.getParamInteger(ConfigKey.Mongo.KEY_mongo_connectTimeout));
                mc.setMaxWaitTime(cfg.getParamInteger(ConfigKey.Mongo.KEY_mongo_maxWaitTime));
                mc.setConnectionPoolSize(cfg.getParamInteger(ConfigKey.Mongo.KEY_mongo_connectionPoolSize));
                mc.setSocketKeepAlive(cfg.getParamBoolean(ConfigKey.Mongo.KEY_mongo_socketKeepAlive));
                mc.setSocketTimeout(cfg.getParamInteger(ConfigKey.Mongo.KEY_mongo_socketTimeout));
                mc.setThreadsAllowedToBlockForConnectionMultiplier(
                        cfg.getParamInteger(ConfigKey.Mongo.KEY_mongo_threadsAllowedToBlockForConnectionMultiplier));
                mongoConfigCached.put(b.getNodeName(), mc);
            }
        }
        
        MongoConfigHolder.put(mongoConfigCached);
        logger.info("mongo配置初始化完成，mongoConfigCached" + mongoConfigCached);
        inited= true;
    }

}
